package com.kc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync
public class AsyncConfig {

    @Bean("Asynchronous")
    public Executor asyncExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //配置核心线程数,线程池创建的时候初始的线程数
        executor.setCorePoolSize(5);
        //最大线程数，当我们用完这个最大线程数的时候，且缓冲队列满的时候线程池会帮我们申请更多的线程去完成任务。
        executor.setMaxPoolSize(5);
        //设置缓冲队列的任务最大总数
        executor.setQueueCapacity(500);
        //允许超过了核心线程数之外的线程最大空闲时间，到时间就会销毁多余的线程 60S
        executor.setKeepAliveSeconds(60);
        //设置线程池前缀名字方便我们查看.
        executor.setThreadNamePrefix("Asynchronous task");

        //初始化设置
        executor.initialize();
        return executor;
    }

}
